package com.xuecheng.manage_media_proces.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ的配置类
 */
@Configuration
public class RabbitMQConfig {
    // 队列bean的名称
    public static final String QUEUE_MEDIA_POSTPAGE = "ex_media_processor";
    // 交换机名称
    public static final String EX_ROUTING_CMS_POSTPAGE = "ex_routing_media_postpage";
    // 视频处理队列名称
    @Value("${xc-service-manage-media.mq.queue-media-video-processor}")
    public String queue_cms_postpage_name;
    // 视频处理路由
    @Value("${xc-service-manage-media.mq.routingkey-media-video}")
    public String routingKey;
    // 消费者并发数量
    public static final int DEFAULT_CONCURRENT = 10;

    // 配置交换机
    @Bean(EX_ROUTING_CMS_POSTPAGE)
    public Exchange EXCHANGE_TOPICS_INFORM(){
        return ExchangeBuilder.directExchange(EX_ROUTING_CMS_POSTPAGE).durable(true).build();
    }
    // 创建队列
    @Bean(QUEUE_MEDIA_POSTPAGE)
    public Queue QUEUE_MEDIA_POSTPAGE(){
        Queue queue = new Queue(queue_cms_postpage_name);
        return queue;
    }
    // 队列绑定交换机
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_MEDIA_POSTPAGE) Queue queue, @Qualifier(EX_ROUTING_CMS_POSTPAGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs();
    }
    @Bean("customContainerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(DEFAULT_CONCURRENT);
        factory.setMaxConcurrentConsumers(DEFAULT_CONCURRENT);
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}
